using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace SmartLink.Infrastructure
{
    /// <summary>
    /// 扩展方法实现的数据验证类
    /// </summary>
    static public class StringPageValidateEx
    {

        /// <summary>
        /// 转换成 HTML code
        /// </summary>
        /// <param name="str">string</param>
        /// <returns>string</returns>
        public static string Encode(this string str)
        {
            if (str != null && str.Length > 0)
            {
                str = str.Replace("&", "&amp;");
                str = str.Replace("'", "''");
                str = str.Replace("\"", "&quot;");
                str = str.Replace(" ", "&nbsp;");
                str = str.Replace("<", "&lt;");
                str = str.Replace(">", "&gt;");
                str = str.Replace("\n", "<br>");
            }
            return str;
        }
        /// <summary>
        ///解析html成 普通文本
        /// </summary>
        /// <param name="str">string</param>
        /// <returns>string</returns>
        public static string Decode(this string str)
        {
            if (str != null && str.Length > 0)
            {
                str = str.Replace("<br>", "\n");
                str = str.Replace("&gt;", ">");
                str = str.Replace("&lt;", "<");
                str = str.Replace("&nbsp;", " ");
                str = str.Replace("&quot;", "\"");
            }
            return str;
        }
        /// <summary>
        /// 清除sql注入符
        /// </summary>
        /// <param name="sqlText"></param>
        /// <returns></returns>
        public static string SqlTextClear(this string sqlText)
        {
            if (sqlText == null)
            {
                return null;
            }
            if (sqlText == "")
            {
                return "";
            }
            sqlText = sqlText.Replace("\"", "");//去除,
            sqlText = sqlText.Replace("<", "");//去除<
            sqlText = sqlText.Replace(">", "");//去除>
            sqlText = sqlText.Replace("--", "");//去除--
            sqlText = sqlText.Replace("'", "");//去除'
            sqlText = sqlText.Replace("\"", "");//去除"
            sqlText = sqlText.Replace("=", "");//去除=
            sqlText = sqlText.Replace("%", "");//去除%
            //sqlText = sqlText.Replace(" ", "");//去除空格
            return sqlText;
        }


        /// <summary>
        /// 检验字符串是否为身份证号         
        /// 需要检验的字符串
        /// 是否为身份证号：true代表是，false代表否
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        //public static bool IsCode(this string str)
        //{
        //    Regex rx = new Regex(@"^[0123456789]{15,18}$");
        //    return rx.IsMatch(str);
        //}


        public static bool IsCode(this string cid)
        {

            cid = cid.ToLower();
            string[] aCity = new string[] { null, null, null, null, null, null, null, null, null, null, null, "北京", "天津", "河北", "山西", "内蒙古", null, null, null, null, null, "辽宁", "吉林", "黑龙江", null, null, null, null, null, null, null, "上海", "江苏", "浙江", "安微", "福建", "江西", "山东", null, null, null, "河南", "湖北", "湖南", "广东", "广西", "海南", null, null, null, "重庆", "四川", "贵州", "云南", "西藏", null, null, null, null, null, null, "陝西", "甘肃", "青海", "宁夏", "新疆", null, null, null, null, null, "台湾", null, null, null, null, null, null, null, null, null, "香港", "澳门", null, null, null, null, null, null, null, null, "国外" };
            double iSum = 0;
            System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(@"^\d{17}(\d|x)$");
            System.Text.RegularExpressions.Match mc = rg.Match(cid);
            if (!mc.Success)
            {
                return false;
            }
            cid = cid.Replace("x", "a");
            if (aCity[int.Parse(cid.Substring(0, 2))] == null)
            {
                return false;
            }
            try
            {
                DateTime.Parse(cid.Substring(6, 4) + "-" + cid.Substring(10, 2) + "-" + cid.Substring(12, 2));
            }
            catch
            {
                return false;
            }
            for (int i = 17; i >= 0; i--)
            {
                iSum += (System.Math.Pow(2, i) % 11) * int.Parse(cid[17 - i].ToString(), System.Globalization.NumberStyles.HexNumber);
            }
            if (iSum % 11 != 1)
            {
                return false;
            }

            return true;

            //return (aCity[int.Parse(cid.Substring(0, 2))] + "," + cid.Substring(6, 4) + "-" + cid.Substring(10, 2) + "-" + cid.Substring(12, 2) + "," + (int.Parse(cid.Substring(16, 1)) % 2 == 1 ? "男" : "女"));

        }



        /// <summary>
        /// 检验字符串是否为URL地址 
        /// 需要检验的字符串
        /// 是否为URL地址：true代表是，false代表否
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsURLAddress(this string str)
        {
            Regex rx = new Regex(@"[a-zA-z]+://[^s]*");
            return rx.IsMatch(str);
        }

        /// <summary>
        /// 检验字符串是否为IP地址 
        /// 需要检验的字符串
        /// 是否为IP地址：true代表是，false代表否
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsIPAddress(this string ip)
        {
            Regex reg = new Regex("(((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))");
            if (reg.IsMatch(ip, 0))
            {
                return (true);
            }
            else
            {
                return (false);
            }
        }

        /// <summary>
        /// 检验字符串是否为邮政编码
        /// 需要检验的字符串
        /// 是否为邮政编码：true代表是，false代表否
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsPostCode(this string str)
        {
            Regex rx = new Regex(@"^[0123456789]{6}$");
            return rx.IsMatch(str);
        }

        /// <summary>
        /// 检验字符串是否为日期时间
        /// 
        /// 需要检验的字符串
        /// 是否为日期时间：true代表是，false代表否
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsDateTime(this string str)
        {
            if (str == null || str.Length < 3)
            {
                return false;
            }
            //Regex rx = new Regex(@"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$");
            //return rx.IsMatch(str);

            DateTime t;
            return DateTime.TryParse(str, out t);

        }

        /// <summary>
        /// 检验字符串是否为日期
        /// 
        /// 需要检验的字符串
        /// 是否为日期时间：true代表是，false代表否
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsDate(this string str)
        {
            if (str == null || str.Length < 3)
            {
                return false;
            }
            Regex rx = new Regex(@"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");
            return rx.IsMatch(str);
        }

        /// <summary>
        /// 是否是邮件
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsEmail(this string inputData)
        {
            if (inputData.Length < 3)
            {
                return false;
            }
            Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info|cn)$");//w 英文字母或数字的字符串，和 [a-zA-Z0-9] 语法一样 
            Match m = RegEmail.Match(inputData);
            return m.Success;
        }

        /// <summary>
        /// 检测是否有中文字符
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>
        public static bool IsHasCHZN(this string inputData)
        {
            Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");
            Match m = RegCHZN.Match(inputData);
            return m.Success;
        }

        /// <summary>
        ///  校验字符串是否只包含字母与数字		 
        /// 需要校验的字符串
        /// true表示符合要求，false表示不符合要求
        /// </summary>
        /// <param name="toVerified"></param>
        /// <returns></returns>
        public static bool IsOnlyLetterAndDigit(this string toVerified)
        {
            Regex rx = new Regex(@"^[a-zA-Z0-9-]*$");
            return rx.IsMatch(toVerified.Trim(), 0);
        }


        /// <summary>
        /// 是否数字字符串(包括小数)
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsNumber(this string inputData)
        {
            if (inputData.IsNonull())
            {
                Regex RegNumber = new Regex(@"^[+-]?[0123456789]*[.]?[0123456789]*$");
                Match m = RegNumber.Match(inputData);
                return m.Success;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 检验是否是整数 
        /// 需要检验的字符串
        /// 是否为整数：true是整数，false非整数
        /// </summary>
        public static bool IsInt(this string str)
        {
            if (str.IsNonull())
            {
                Regex rx = new Regex(@"^[0123456789]+$");
                return rx.IsMatch(str);
            }
            else
            {
                return false;
            }

        }

        /// <summary>
        /// 是否数字字符串 可带正负号
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsNumberSign(this string inputData)
        {
            Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
            Match m = RegNumberSign.Match(inputData);
            return m.Success;
        }
        /// <summary>
        /// 是否是浮点数
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsDecimal(this string inputData)
        {
            //Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
            Regex RegDecimal = new Regex(@"^[0-9]*(.)?[0-9]+$", RegexOptions.IgnoreCase);
            Match m = RegDecimal.Match(inputData);
            return m.Success;
        }
        /// <summary>
        /// 是否是浮点数 可带正负号
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsDecimalSign(this string inputData)
        {
            Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$
            Match m = RegDecimalSign.Match(inputData);
            return m.Success;
        }


        /// <summary>
        /// 验证是否经纬度
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>
        public static bool IsLat(this string inputData)
        {
            Regex RegDecimalSign = new Regex(@"^(-?((180)|(((1[0-7]\d)|(\d{1,2}))(\.\d+)?))),(-?((180)|(((1[0-7]\d)|(\d{1,2}))(\.\d+)?)))$");
            Match m = RegDecimalSign.Match(inputData);
            return m.Success;
        }


        /// <summary>
        /// 是否是手机电话号码
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>
        public static bool IsMobliPhone(this string inputData)
        {
            Regex RegPhone = new Regex("^((\\+86)|(86))?(1)\\d{10}$");
            Match m = RegPhone.Match(inputData);
            return m.Success;
        }

        /// <summary>
        /// 确定request对象是否有值(true  有值 false 无值)
        /// </summary>
        /// <param name="inputData">true  有值 false 无值</param>
        /// <returns></returns>
        public static bool IsNonull(this string inputData)
        {
            if (inputData == null || inputData == "")
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
